package org.wuyichun.course.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wuyichun.base.util.AjaxResult;
import org.wuyichun.course.domain.Comment;
import org.wuyichun.course.service.ICommentService;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
@Api("/评论相关的Controller")
public class CommentController {
    @Autowired
    private ICommentService commentService;

    // 新增评论 内容 类型  用户 产品id
    @PostMapping
    @ApiOperation(value = "添加评论")
    public AjaxResult addComment(@RequestBody Comment comment){
        try {
            commentService.add(comment);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("评论失败");
        }
    }
    // 根据课程查询评论
    @ApiOperation(value = "查询评论")
    @GetMapping("/{id}")
    public AjaxResult getCommentByCourseId(@PathVariable("id") Long id){
        try {
            List<Comment> comments = commentService.getCommentByCourseId(id);
            return AjaxResult.me().setResultObj(comments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取评论失败");
        }
    }
    // 回复评论  回复的内容 评论id 回复人
    @ApiOperation(value = "回复评论")
    @PostMapping("/{id}")
    public AjaxResult replyComment(@RequestBody Map<String,Object> map){
        try {
            commentService.replyComment(map);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("回复评论失败");
        }
    }
    @ApiOperation(value = "删除评论")
    // 删除评论
    @DeleteMapping("/{id}")
    public AjaxResult deleteComment(@PathVariable("id") Long id){
        try {
            commentService.deleteComment(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除评论失败");
        }
    }
    @ApiOperation(value = "删除回复")
    // 删除回复 回复id， 评论id
    @DeleteMapping("/deleteReply")
    public AjaxResult deleteReply(@RequestBody Map<String,Object> map){
        try {
            commentService.deleteReply(map);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除回复失败");
        }
    }
}
